// JavaScript Document
$(function () {
    nowIdx();
    $(".nav__ul .nav__li").hover(function () {
        var idx =  $(this).index();
        var text =  $(this).find("a:first-child").text();
        $(".nav__line").stop().animate({"left":96*idx},300);
        if(text === "云市场"){
            $(this).find(".subNav").stop().slideDown();
        }
    },function () {
        nowIdx();
        $(this).find(".subNav").stop().slideUp();
    });
    
    $(".headIcon").hover(function () {
        $(".operate").stop().fadeIn();
        $(this).find(".icArrow").addClass("rotate_x180");
    },function () {
        $(".operate").stop().fadeOut();
        $(this).find(".icArrow").removeClass("rotate_x180");
    })
});
//导航栏动画
var nowIdx = function(){
    var c_idx = $(".nav__ul li.current").index();
    $(".nav__line").stop().animate({"left":96*c_idx});
};
//首页初始化
var indexInit = function() {
    var scale = $(window).width()/1920,
        contTop=$(".container").offset().top,
        isFirst = true;
    $(".banner__ul").height(860*scale);
    //banner
    var slide = {
        idx:0,
        timer:null,
        li_num:$(".banner__ul .banner__li").length,
        btn1:$(".banner__prev"),
        btn2:$(".banner__next"),
        btn3:$(".banner__dotul .banner__dotli"),
        init:function () {
            var that = this;
            this.btn1.click(function () {
                that.prev(true);
            });
            this.btn2.click(function () {
                that.next(true);
            });
            this.btn3.click(function () {
                that.idx = $(this).index();
                that.slideBan(function(){
                    that.setInter();
                });
            });
            that.setInter();
        },
        prev:function (flag) {
            if(this.idx>0){
                this.idx--;
            }else{
                this.idx = this.li_num-1;
            }
            if(flag){
                this.slideBan(this.setInter());
            }else{
                this.slideBan();
            }
        },
        next:function(flag){
            if(this.idx<this.li_num-1){
                this.idx++;
            }else{
                this.idx = 0;
            }
            if(flag){
                this.slideBan(this.setInter());
            }else{
                this.slideBan();
            }
        },
        setInter:function(){
            clearInterval(this.timer);
            var that = this;
            this.timer = setInterval(function () {
                that.next();
            },5000);
        },
        slideBan:function (cb) {
            var num = this.idx;
            $(".banner__ul .banner__li").eq(num).addClass("transx").siblings().removeClass("transx");
            $(".banner__text").removeClass("cur_bt");
            $(".banner__ul .banner__li").eq(num).find(".banner__text").addClass("cur_bt");
            this.btn3.eq(num).addClass("banner__curdot").siblings().removeClass("banner__curdot");
            if(cb){
                cb();
            }
        }
    };
    slide.init();
    $(".pro__tab li").on("click",function(){
        var idx = $(this).index();
        $(this).addClass("cur_tab").siblings().removeClass("cur_tab");
        $(".cont2__list li.cl_li").eq(idx).show().siblings().hide();
    });
    $(document).scroll(function() {
        //获取滚动条滚动的高度
        var scroltop = $(document).scrollTop();
        if(scroltop >= (contTop/3) && isFirst){
            isFirst = false;
            numAdd("num1",{time: 1500, regulator: 50});
            numAdd("num2",{time: 2000, regulator: 50});
            numAdd("num3",{time: 2500, regulator: 50});
        }
    });

};
//产品列表初始化
var proInit = function () {
    var pSlide = {
        idx:0,
        timer:null,
        li_num:$(".pro_box_ul .pro__slide").length,
        btn:$(".pro__dot li"),
        init:function () {
            var that = this;
            this.btn.click(function () {
                that.idx = $(this).index();
                that.slide(function(){
                    that.setInter();
                });
            });
            that.setInter();
        },
        setInter:function(){
            clearInterval(this.timer);
            var that = this;
            this.timer = setInterval(function () {
                that.next();
            },5000);
        },
        next:function(){
            if(this.idx<this.li_num-1){
                this.idx++;
            }else{
                this.idx = 0;
            }
            this.slide();
        },
        slide:function (cb) {
            var num = this.idx;
            $(".pro_box_ul .pro__slide").eq(num).addClass("transxs").siblings().removeClass("transxs");
            this.btn.eq(num).addClass("cur").siblings().removeClass("cur");
            if(cb){
                cb();
            }
        }
    };
    pSlide.init();
    tabSlide();
    var urlSh = window.location.search,
        curLi = urlSh.substring(urlSh.indexOf("=")+1);
    $(".pro__tab .tabLi").eq(curLi).addClass("cur_tab").siblings().removeClass("cur_tab");
};
//产品页tab
var tabSlide = function () {
    $(".pro__tab .tabLi").hover(function(){
        var idx = $(this).index();
        $(".subTab").hide();
        $(this).find(".subTab").stop().slideDown(500);

    },function () {
        $(this).find(".subTab").stop().slideUp(200);
    });
};
//安全设置初始化
var setInit = function () {
    var g1 = document.getElementById("get_code1"),
        g2 = document.getElementById("get_code2");
    g1.onclick = function () {
        timeCount(this,1);
    };
    g2.onclick = function () {
        timeCount(this,1);
    };
};
//注册页初始化
var regInit = function(){
    $("#agree").on("click",function(){
        var checked = $(this).is(":checked");
        if(checked){
            $("#submit").removeAttr("disabled").addClass("bc-blue");
        }else{
            $("#submit").prop("disabled",true).removeClass("bc-blue");
        }
    });
    $("#getCode").on("click",function(){
        timeCount(this,2);
    });
};
//检验格式
var testInput = function(id){
    var reg =  /^(((13[0-9]{1})|(15[0-9]{1})|(17[0-9])|(18[0-9]{1}))+\d{8})$/,
        obj = document.getElementById(id),
        ic = obj.nextElementSibling;
    if(id === "mail"){
        reg =  /^([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$/;
    }
    if(reg.test(obj.value)){
        obj.classList.remove("bd_error");
        ic.className="ic_pass";
    }else{
        obj.className+=" bd_error";
        ic.className="ic_fail";
    }
};
//检验两次密码是否一致
var testPsw = function (id1,id2) {
    var obj1 = document.getElementById(id1),
        obj2 = document.getElementById(id2),
        ic1 = obj1.nextElementSibling,
        ic2 = obj2.nextElementSibling;
    if(obj2.value){
        if(obj1.value === obj2.value){
            obj1.classList.remove("bd_error");
            obj2.classList.remove("bd_error");
            ic1.className="ic_pass";
            ic2.className="ic_pass";
        }else{
            obj1.className+=" bd_error";
            ic1.className="ic_fail";
        }
    }
};
//数字动态增加
var numAdd = function(el,opts){
    opts = opts || {};
    var $t = document.getElementById(el);
    var time = opts.time || $t.data('time'), //总时间--毫秒为单位
        finalNum = parseInt($t.innerHTML), //要显示的真实数值
        regulator = opts.regulator || 100, //调速器，改变regulator的数值可以调节数字改变的速度
        step = finalNum / (time / regulator),/*每30ms增加的数值--*/
        count = 0, //计数器
        initial = 0;
    var timer = setInterval(function() {
        count += step;
        if(count >= finalNum) {
            clearInterval(timer);
            count = finalNum;
        }
        //t未发生改变的话就直接返回
        var t = Math.floor(count);
        if(t == initial) return;
        initial = t;
        $t.innerHTML = initial;
    }, 30);
};
//倒计时
var timeCount = function (obj,flag) {
    var time=60;
    var that=obj;
    var timer=setInterval(function(){
        if(time<=0){
            that.innerText="";
            that.innerText="重新发送";
            if(flag === 1){
                that.style.color = "";
            }else{
                that.style.background = "";
            }
            that.disabled=false;
            clearInterval(timer);//清除计时器
        }else {
            that.disabled=true;
            if(flag === 1){
                that.style.color = "#999";
            }else{
                that.style.background = "#999";
            }
            that.innerText="重新发送("+time+")秒";
            time--;
        }
    },1000);
};